package JDBC;

import Util.ClassInfo;
import Util.Dish;
import Util.Package;
import Util.Staff;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {

    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    private Long id = null;//当前登入的账号id
    private String sql = null; //数据库操作语句
    private Staff sta = null;
    Scanner sc = new Scanner(System.in);


    /**
     * 用户名，密码赋值
     */
    public void loginMenu(){
        System.out.println("======登入菜单======");
        while (true) {
            System.out.println("请输入用户名：");
            String username = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            if (okLogin(username, password)) {
                mainMenu();
                DBUtil.close(conn,pstmt,rs);
                break;
            }
        }
    }

    /**
     * 链接数据库，判断账号密码
     * @param username 用户名
     * @param password 密码
     * @return 返回判断
     */
    public boolean okLogin(String username, String password){
        try {
            conn = DBUtil.getConnection();
            String sql ="select * from `employee` where username = ? and password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            rs = pstmt.executeQuery();

            if (rs.next()){
                if (rs.getInt("status") != 1 ){
                    System.out.println("您的账号已被禁用");
                    return false;
                }
                id = rs.getLong(1);
                System.out.println("欢迎" + rs.getString(2) + "回来~~");
                return true;

            }
            else{
                System.out.println("账号或者密码错误~~~");
                return false;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //员工管理,分类管理,菜品管理,套餐管理,订单管理,订单明细管理
    public void mainMenu(){
        while (true) {
            System.out.println("======管理菜单======");
            System.out.println("1.员工管理");
            System.out.println("2.分类管理");
            System.out.println("3.菜品管理");
            System.out.println("4.套餐管理");
            System.out.println("5.订单管理");
            System.out.println("6.订单明细管理");
            System.out.println("7.退出登入");
            System.out.println("请输入对应操作");
            switch (sc.next()){
                case "1":
                    employeeManagement();
                    break;
                case "2":
                    classManagement();
                    break;
                case "3":
                    dishManagement();
                    break;
                case "4":
                    packageManagement();
                    break;
                case "5":
                    orderManagement();
                    break;
                case "6":
                    orderDetailsManagement();
                    break;
                case "7":
                    DBUtil.close(conn,pstmt,rs);
                    return;
                default:
                    System.out.println("输入错误，请重新输入~~");

            }
        }


    }

    ////员工管理,分类管理,菜品管理,套餐管理,订单管理,订单明细管理

    /**
     * 菜单页面
     */
    public void employeeManagement(){
        while (true) {
            System.out.println("======员工管理菜单======");
            System.out.println("1.添加员工");
            System.out.println("2.修改员工");
            System.out.println("3.删除员工");
            System.out.println("4.查询员工");
            System.out.println("5.退出");
            switch (sc.next()){
                case "1":
                    sql = "INSERT INTO employee VALUES(?,?,?,?,?,?,?,1,?,?,?,?)";
                    sta = new Staff();
                    sta = employeeInformationAdd();
                    add(sta,sql);
                    break;
                case "2":
                    employeeInformationModify();
                    break;
                case "3":
                    employeeDeleteById();
                    break;
                case "4":
                    employeeQueryById();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误，重新输入~~");

            }
        }
    }

    /**
     * 分类管理菜单
     */
    public void classManagement(){
        while (true) {
            System.out.println("======分类管理菜单======");
            System.out.println("1.添加分类");
            System.out.println("2.修改分类");
            System.out.println("3.删除分类");
            System.out.println("4.查询分类");
            System.out.println("5.退出");
            switch (sc.next()){
                case "1":
                    classAdd();
                    break;
                case "2":
                    classModifyById();
                    break;
                case "3":
                    classDeleteById();
                    break;
                case "4":
                    classQueryById();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误，重新输入~~");

            }
        }
    }


    /**
     * 查询分类
     */
    private void classQueryById() {

        String sql;
        try {
            sql = "select * from class";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                ClassInfo ci = new ClassInfo(rs.getString(1), rs.getString(2), rs.getLong(3),rs.getString(4), rs.getString(5), rs.getLong(6), rs.getLong(7));

                System.out.println(ci);
            }
        } catch (SQLException e) {
            System.out.println("没有数据~~~");
        }
    }


    /**
     * 删除分类信息
     */
    private void classDeleteById() {

        String sql;
        String id;
        System.out.println("=======删除分类信息======");
        while (true) {
            System.out.println("请输入要删除的分类名称~~~");
            id = sc.next();
            try {
                sql = "select * from class where types_name = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,id);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该id~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该id~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                sql = "delete from class where types = "+ id;
                pstmt = conn.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("删除失败");
                }
                else{
                    System.out.println("删除成功");
                    break;
                }

            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }


    /**
     * 修改分类
     */
    private void classModifyById() {
        String sql;
        String classId;
        String column;
        String data;
        System.out.println("=======修改分类信息======");
        while (true) {
            System.out.println("请输入要修改的分类名称~~~");
            classId = sc.next();
            try {
                sql = "select * from class where types_name = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,classId);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该分类~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该分类~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                String upDateTime = MerchantRegister.getDataTime();
                System.out.println("请输入修改的列名~~");
                column = sc.next();
                System.out.println("请输入修改的值~~");
                data = sc.next();

                sql = "update class set "+ column +" = ?, up_time = ?, update_user = ? where types_name = "+ classId;
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,data);
                pstmt.setString(2, upDateTime);
                pstmt.setLong(3, this.id);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("修改失败");
                }
                else{
                    System.out.println("修改成功");
                    break;
                }

            } catch (SQLException e) {
                System.out.println("列名或数值错误错误，请重新输入~~");
                //e.printStackTrace();
            }
        }

    }


    /**
     * 添加分类
     */
    public void classAdd(){
        String type;
        String typeName;
        Long order = null;
        String creatTime;
        String updateTime;
        Long creatUser;
        Long updateUser;
        String sql;
        System.out.println("=======添加分类信息======");

        while (true) {
            System.out.println("请输入分类的类型：");
            try {
                type = sc.next();

                if(type.length()<32){
                    break;
                }
                else
                    System.out.println("类型过长，请重新输入");
            } catch (Exception e) {
                System.out.println("输入类型错误，必须为数字");
            }
        }

        while (true) {
            System.out.println("请输入分类的名称：");
            try {
                typeName = sc.next();
                sql = "select * from class where types_name = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, typeName);
                rs = pstmt.executeQuery();
                if (rs.next()){
                    System.out.println("分类名称重复，请重新输入~~~");
                    break;
                }
                if(typeName.length()<32){
                    break;
                }
                else
                    System.out.println("名称过长，请重新输入");
            } catch (Exception e) {
                System.out.println("输入类型错误");
                e.printStackTrace();
            }
        }
        creatTime = MerchantRegister.getDataTime();
        updateTime = MerchantRegister.getDataTime();
        creatUser = this.id;
        updateUser = this.id;
        sql = "INSERT INTO class(types,types_name,create_time,up_time,create_user,update_user) values (?,?,?,?,?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, type);
            pstmt.setString(2, typeName);

            pstmt.setString(3, creatTime);
            pstmt.setString(4, updateTime);
            pstmt.setLong(5, creatUser);
            pstmt.setLong(6, updateUser);
            int i = pstmt.executeUpdate();
            if (i>0){
                System.out.println("添加成功~~");

            }
            else
                System.out.println("添加失败~~");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }


    /**
     *
     * 分割线
     * --------------------------------------------------------------------------------------------------------------------------*/
    public void dishManagement(){
        while (true) {
            System.out.println("======菜品管理菜单======");
            System.out.println("1.添加菜品");
            System.out.println("2.修改菜品");
            System.out.println("3.删除菜品");
            System.out.println("4.查询菜品");
            System.out.println("5.退出");
            switch (sc.next()){
                case "1":
                    dishAdd();
                    break;
                case "2":
                    dishModifyById();
                    break;
                case "3":
                    dishDeleteById();
                    break;
                case "4":
                    dishQuery();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误，重新输入~~");

            }
        }
    }

    public void dishModifyById() {
        String dishId;
        String data;
        String column;
        System.out.println("=======修改菜品信息======");
        while (true) {
            System.out.println("请输入要修改的菜品的商品码~~~");
            dishId = sc.next();
            try {
                sql = "select * from dish where dish_id = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,dishId);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该菜品~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该菜品~~,请重新输入~~~");
                e.printStackTrace();
            }
        }
        while (true) {
            try {
                String upDateTime = MerchantRegister.getDataTime();
                System.out.println("请输入修改的列名~~");
                column = sc.next();
                System.out.println("请输入修改的值~~");
                data = sc.next();

                sql = "update dish set "+ column +" = ?, up_time = ?, update_user = ? where dish_id = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,data);
                pstmt.setString(2, upDateTime);
                pstmt.setLong(3, this.id);
                pstmt.setString(4, dishId);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("修改失败");
                }
                else{
                    System.out.println("修改成功");
                    break;
                }

            } catch (SQLException e) {
                System.out.println("列名或数值错误错误，请重新输入~~");
                e.printStackTrace();
            }
        }


    }

    public void dishDeleteById() {

        String sql;
        String id;
        System.out.println("=======删除菜品信息======");
        while (true) {
            System.out.println("请输入要删除的菜品商品码~~~");
            id = sc.next();
            try {
                sql = "select * from dish where dish_id = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,id);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该id~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该id~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                sql = "delete from dish where dish_id = "+ id;
                pstmt = conn.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("删除失败");
                }
                else{
                    System.out.println("删除成功");
                    break;
                }

            } catch (SQLException e) {

                e.printStackTrace();
            }
        }

    }

    public void dishQuery() {

        String sql;
        try {
            sql = "select * from dish";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Dish dish = new Dish(rs.getString(1), rs.getString(2), rs.getBigDecimal(3),rs.getLong(4), null, rs.getString(6), rs.getInt(7), rs.getLong(8), rs.getString(9), rs.getString(10), rs.getLong(11), rs.getLong(12), rs.getInt(13));

                System.out.println(dish);
            }
        } catch (SQLException e) {
            System.out.println("没有数据~~~");
            e.printStackTrace();
        }
    }


    /**
     * 菜品添加

     */
    public void dishAdd(){
        String name;
        String classId;
        BigDecimal price;
        Long id;//商品码
        String img;//暂时没有
        String describe = null; //描述信息
        int state;
        Long order;
        String creatTime;
        String updateTime;
        Long creatUser;
        Long updateUser;
        String sql;
        System.out.println("=======添加菜品信息======");
        while (true) {
            System.out.println("请输入菜品的名称：");

            name = sc.next();

            if (name.length() < 32) {
                break;
            } else
                System.out.println("名称过长，请重新输入");
        }
        while (true) {
            System.out.println("请输入菜品的分类名称：");

            try {
                sql = "select * from class";
                pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs = pstmt.executeQuery();
                if (!rs.next()){
                    System.out.println("没有分类数据，请进行添加~~");
                    return;
                }
                rs.beforeFirst();
                while (rs.next()) {
                    System.out.println(rs.getInt(3) + rs.getString(2));
                }
                classId = sc.next();
                sql = "select * from class where types_name = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, classId);
                rs = pstmt.executeQuery();
                if (rs.next()){
                    break;
                }
                else {
                    System.out.println("输入类型不存在，请重新输入~~~");
                }
            } catch (SQLException e) {
                System.out.println("输入类型错误，请重新输入~~~");
                e.printStackTrace();
            }

        }


        System.out.println("请输入菜品的价格：");

        price = new BigDecimal(String.valueOf(sc.nextBigDecimal()));
        price = price.setScale(2);


        id = new Long("9" + MerchantRegister.getId());

        while (true) {
            System.out.println("请输入菜品的描述信息：");

            describe = sc.next();

            if (describe.length() < 32) {
                break;
            } else
                System.out.println("名称过长，请重新输入");
        }

        while (true) {
            System.out.println("请输入菜品的状态：0.禁售     1.开售  ");
            try {
                state = sc.nextInt();
                if (state == 1 || state == 0)
                    break;
                else
                    System.out.println("输入错误");
            } catch (Exception e) {
                System.out.println("输入错误");
                throw new RuntimeException(e);
            }
        }

        creatTime = MerchantRegister.getDataTime();
        updateTime = MerchantRegister.getDataTime();
        creatUser = this.id;
        updateUser = this.id;
        sql = "INSERT INTO dish VALUES(?,?,?,?,null,?,?,?,?,?,?,?,?); ";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2, classId);
            pstmt.setBigDecimal(3,price);
            pstmt.setLong(4, id);
            pstmt.setString(5, describe);
            pstmt.setInt(6,state);
            pstmt.setLong(7, id);
            pstmt.setString(8, creatTime);
            pstmt.setString(9,updateTime);
            pstmt.setLong(10, creatUser);
            pstmt.setLong(11, updateUser);
            pstmt.setInt(12, 0);
            int i = pstmt.executeUpdate();
            if (i > 0){
                System.out.println("创建成功");
            }
            else
                System.out.println("创建失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 套餐菜单
     * * -----------------------------------------------------------------------------------------------------------------------/
     */
    public void packageManagement(){

        while (true) {
            System.out.println("======套餐管理菜单======");
            System.out.println("1.添加套餐");
            System.out.println("2.修改套餐");
            System.out.println("3.删除套餐");
            System.out.println("4.查询套餐");
            System.out.println("5.退出");
            switch (sc.next()){
                case "1":
                    packageAdd();
                    break;
                case "2":
                    packageModifyById();
                    break;
                case "3":
                    packageDeleteById();
                    break;
                case "4":
                    packageQuery();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误，重新输入~~");

            }
        }

    }

    private void packageQuery() {

        String sql;
        try {
            sql = "select * from package";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){

                Package pack = new Package(rs.getString(1), rs.getString(2), rs.getBigDecimal(3), rs.getInt(4), rs.getLong(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9),rs.getLong(10), rs.getLong(11), rs.getInt(12));
                System.out.println(pack);
            }
        } catch (SQLException e) {
            System.out.println("没有数据~~~");
            e.printStackTrace();
        }
    }

    private void packageDeleteById() {

        String sql;
        String code;
        System.out.println("=======删除套餐信息======");
        while (true) {
            System.out.println("请输入要删除的菜品商品码~~~");
            code = sc.next();
            try {
                sql = "select * from package where code = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,code);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该编码~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该编码~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                sql = "delete from package where code = "+ code;
                pstmt = conn.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("删除失败");
                }
                else{
                    System.out.println("删除成功");
                    break;
                }

            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }

    private void packageModifyById() {

        String code;
        String data;
        String column;
        System.out.println("=======修改套餐信息======");
        while (true) {
            System.out.println("请输入要修改的套餐的编码~~~");
            code = sc.next();
            try {
                sql = "select * from package where code= ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,code);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该菜品~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该菜品~~,请重新输入~~~");
                e.printStackTrace();
            }
        }
        while (true) {
            try {
                String upDateTime = MerchantRegister.getDataTime();
                System.out.println("请输入修改的列名~~");
                column = sc.next();
                System.out.println("请输入修改的值~~");
                data = sc.next();

                sql = "update package set "+ column +" = ?, update_time = ?, update_user = ? where code = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,data);
                pstmt.setString(2, upDateTime);
                pstmt.setLong(3, this.id);
                pstmt.setString(4, code);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("修改失败");
                }
                else{
                    System.out.println("修改成功");
                    break;
                }

            } catch (SQLException e) {
                System.out.println("列名或数值错误错误，请重新输入~~");
                e.printStackTrace();
            }
        }


    }

    private void packageAdd() {

        String type; //分类名称
        String name;
        BigDecimal price;
        int state;
        Long code;//编码
        String describe = null; //描述信息
        String img;//暂时没有

        String creatTime;
        String updateTime;
        Long creatUser;
        Long updateUser;
        int dele = 0;
        String sql;
        System.out.println("=======添加套餐信息======");
        while (true) {
            System.out.println("请输入套餐的名称：");

            name = sc.next();

            if (name.length() < 32) {
                break;
            } else
                System.out.println("名称过长，请重新输入");
        }

        while (true) {
            System.out.println("请输入套餐的分类名称：");

            try {
                sql = "select * from class";
                pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs = pstmt.executeQuery();
                if (!rs.next()){
                    System.out.println("没有分类数据，请进行添加~~");
                    return;
                }
                rs.beforeFirst();
                while (rs.next()) {
                    System.out.println(rs.getInt(3) + rs.getString(2));
                }
                type = sc.next();
                sql = "select * from class where types_name = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, type);
                rs = pstmt.executeQuery();
                if (rs.next()){
                    break;
                }
                else {
                    System.out.println("输入类型不存在，请重新输入~~~");
                }
            } catch (SQLException e) {
                System.out.println("输入类型错误，请重新输入~~~");
                e.printStackTrace();
            }

        }

        System.out.println("请输入套餐的价格：");

        price = new BigDecimal(sc.next());
        price = price.setScale(2, BigDecimal.ROUND_HALF_UP);

        while (true) {
            System.out.println("请输入菜品的状态：0.禁售     1.开售  ");
            try {
                state = sc.nextInt();
                if (state == 1 || state == 0)
                    break;
                else
                    System.out.println("输入错误");
            } catch (Exception e) {
                System.out.println("输入错误");
                throw new RuntimeException(e);
            }
        }
        code = new Long("9" + MerchantRegister.getId());

        while (true) {
            System.out.println("请输入菜品的描述信息：");

            describe = sc.next();

            if (describe.length() < 32) {
                break;
            } else
                System.out.println("名称过长，请重新输入");
        }

        creatTime = MerchantRegister.getDataTime();
        updateTime = MerchantRegister.getDataTime();
        creatUser = this.id;
        updateUser = this.id;
        sql = "INSERT INTO package VALUES(?,?,?,?,?,?,null,?,?,?,?,?); ";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,type);
            pstmt.setString(2, name);
            pstmt.setBigDecimal(3,price);
            pstmt.setInt(4, state);

            pstmt.setLong(5,code);
            pstmt.setString(6, describe);

            pstmt.setString(7, creatTime);
            pstmt.setString(8,updateTime);
            pstmt.setLong(9, creatUser);
            pstmt.setLong(10, updateUser);
            pstmt.setInt(11, 0);
            int i = pstmt.executeUpdate();
            if (i > 0){
                System.out.println("创建成功");
            }
            else
                System.out.println("创建失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     *
     * * -----------------------------------------------------------------------------------------------------------------------/
     */
    public void orderManagement(){

    }
    public void orderDetailsManagement(){

    }

    //添加JDBC

    /**
     *员工表增加
     * @param sta
     * @param sql
     */
    public void add(Staff sta, String sql){
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1,sta.getId());
            pstmt.setString(2,sta.getName());
            pstmt.setString(3,sta.getUsername());
            pstmt.setString(4,sta.getPassword());
            pstmt.setString(5,sta.getPhone());
            pstmt.setString(6,sta.getSex());
            pstmt.setString(7,sta.getIdNumber());
            pstmt.setString(8, sta.getCreatDateTime());
            pstmt.setString(9,sta.getUpdateDateTime());
            pstmt.setLong(10,sta.getCreatUser());
            pstmt.setLong(11,sta.getUpdateUser());
            int i = pstmt.executeUpdate();
            if (i != 0){
                System.out.println("添加成功~~~");
                System.out.println("输入任意字符返回上一级菜单~~");
                sc.next();
            }
            else
                System.out.println("添加失败~~~");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * 员工信息增加
     * @return
     */
    public Staff employeeInformationAdd(){
        Long staffId;
        String name;
        String username;
        String password;
        String phone;
        String sex;
        String idNumber;
        Integer stat = 1;
        String creatDateTime = MerchantRegister.getDataTime();
        String updateDateTime = MerchantRegister.getDataTime();
        Long creatUser = id;
        Long updateUser = id;
        staffId = new Long(MerchantRegister.getId() + "2");
        System.out.println("=======添加员工======");
        System.out.println("请输入姓名：");
        while (true) {
            name = sc.next();
            if (name.length()>7)
                System.out.println("姓名过长，请重新输入：");
            else
                break;
        }
        System.out.println("请输入用户名：");

        while (true) {
            username = new Scanner(System.in).next();
            if(username.length()>32)
                System.out.println("用户名过长，请重新输入：");
            else{
                try {
                    String sql = "select * from employee where username = ?";
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1,username);
                    rs = pstmt.executeQuery();
                    if (!rs.next()){
                        break;
                    }
                    else
                        System.out.println("用户名重复，请重新输入：");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("请输入密码：");
        while (true) {
            password = sc.next();
            if(password.length()>32)
                System.out.println("密码过长，请重新输入：");
            else
                break;
        }
        System.out.println("请确认密码：");
        while (true) {
            String password2 = sc.next();
            if(!password2.equals(password))
                System.out.println("两次密码不一致，请重新输入：");
            else
                break;
        }
        System.out.println("请输入手机号：");
        while (true) {
            Scanner s = new Scanner(System.in);
            try {
                phone = "" + s.nextLong();
                if(phone.length()!=11)
                    System.out.println("手机号错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                phone = null;
                System.out.println("手机号错误，请重新输入：");
            }

        }
        System.out.println("请输入性别：");
        while (true) {
            sex = sc.next();
            if(sex.equals("女") && sex.equals("男"))
                System.out.println("性别输入错误，请重新输入：");
            else
                break;
        }
        System.out.println("请输入身份证号码：");
        while (true) {
            Scanner s = new Scanner(System.in);
            idNumber = null;
            try {
                idNumber = "" + s.nextLong();

                if(idNumber.length() != 18)
                    System.out.println("身份证号输入错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                System.out.println("身份证号码输入错误，请重新输入：");

            }

        }

        Staff sta = new Staff(staffId, name, username,password, phone, sex, idNumber, stat, creatDateTime, updateDateTime, creatUser, updateUser);
        return sta;
    }


    /**
     * 根据员工id修改员工信息
     * @return
     */
    public void employeeInformationModify(){
        String sql;
        String id;
        String column;
        String data;
        while (true) {
            System.out.println("请输入要修改的员工id~~~");
            id = sc.next();
            try {
                sql = "select * from employee where id = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,id);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该id~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该id~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                String upDateTime = MerchantRegister.getDataTime();
                System.out.println("请输入修改的列名~~");
                column = sc.next();
                System.out.println("请输入修改的值~~");
                data = sc.next();
                if (column.equals("sex")){
                    if (!data.equals("女") && !data.equals("男")){
                        System.out.println("输入的数据不正确，只允许输入男或女");
                        continue;
                    }
                }
                sql = "update employee set "+ column +" = ?, update_time = ?, update_user = ? where id = "+ id;
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,data);
                pstmt.setString(2, upDateTime);
                pstmt.setLong(3, this.id);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("修改失败");
                }
                else{
                    System.out.println("修改成功");
                    break;
                }

            } catch (SQLException e) {
                System.out.println("列名或数值错误错误，请重新输入~~");
               // e.printStackTrace();
            }
        }
    }


    /**
     * 根据id删除员工
     */
    public void employeeDeleteById(){
        String sql;
        String id;
        while (true) {
            System.out.println("请输入要删除的员工id~~~");
            id = sc.next();
            try {
                sql = "select * from employee where id = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,id);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    break;
                }

                else
                    System.out.println("不存在该id~~,请重新输入~~~");
            } catch (SQLException e) {
                System.out.println("不存在该id~~,请重新输入~~~");
            }
        }
        while (true) {
            try {
                sql = "delete from employee where id = "+ id;
                pstmt = conn.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                if (i<1) {
                    System.out.println("删除失败");
                }
                else{
                    System.out.println("删除成功");
                    break;
                }

            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }


    /**
     * 展示全部员工信息
     */
    public void employeeQueryById(){
        String sql;
        try {
            sql = "select * from employee";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Staff sta = new Staff(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getInt(8),rs.getString(9), rs.getString(10), rs.getLong(11), rs.getLong(12));
                System.out.println(sta);
            }
        } catch (SQLException e) {
            System.out.println("没有数据~~~");
        }

    }



}

